/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TwoFaceFrameConsulta.java
 *
 * Created on 19/01/2010, 10:13:31
 */

package calpi.gui;

import calpi.Connectors;
import calpi.Curso;
import calpi.Disciplina;
import calpi.database.CursosConnector;
import calpi.database.DataBaseConnector;
import calpi.database.DisciplinasConnector;
import calpi.util.Comparadores;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import static javax.swing.JOptionPane.*;

/**
 *
 * @author Sem iNick
 */
public class TwoFaceFrameConsulta extends javax.swing.JFrame {

    public static final byte DISCIPLINAS = 0;

    public static final byte CURSOS = 1;

    //Guarda a referência para a janela que voltará quando está for fechada
    private Principal parent = null;
    private byte tipo = 0;
    private DataBaseConnector con = null;
    //private DisciplinasConnector dc = null;
    
    /** Creates new form TwoFaceFrameConsulta */
    public TwoFaceFrameConsulta(Principal pai, byte tipo) {

        if ((tipo != DISCIPLINAS) && (tipo != CURSOS)) {

            throw new IllegalArgumentException("Tipo de janela inválido.");
        }

        parent = pai;
        initComponents();
        this.tipo = tipo;
        
        if (tipo == CURSOS) {

            jLabel1.setText("Cursos cadastrados");
            con  = (CursosConnector) Connectors
                    .getConnector(Connectors.CURSOS);
            con.setComparator(Comparadores.ComparaCurso.NOME);
        } else {

            jLabel1.setText("Disciplinas cadastradas");
            con  = (DisciplinasConnector) Connectors
                    .getConnector(Connectors.DISCIPLINAS);
            con.setComparator(Comparadores.ComparaDisciplina.DESCRICAO);
        }

        refreshList();
    }

    public void refreshList() {


        Object[] objects = null;
        con.sortBufferOut();
        objects = con.getBufferOut();

        if (objects == null) {

            return;
        }

        Object[] nomes = new Object[objects.length];
        for (int a = 0; a < objects.length; a++) {

            if (tipo == CURSOS) {

                nomes[a] = ((Curso) objects[a]).getNome();
            } else {

                nomes[a] = ((Disciplina) objects[a]).getDescricao();
            }
        }

        list.setListData(nomes);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        list = new javax.swing.JList();
        editar = new javax.swing.JButton();
        excluir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setLocationByPlatform(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Disciplinas Cadastradas");

        list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        list.setPreferredSize(new java.awt.Dimension(100, 300));
        jScrollPane1.setViewportView(list);

        editar.setText("Editar");
        editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editarActionPerformed(evt);
            }
        });

        excluir.setText("Excluir");
        excluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(editar)
                        .addGap(27, 27, 27)
                        .addComponent(excluir))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jLabel1)))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(excluir)
                    .addComponent(editar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing

        setVisible(false);
        //Libera recursos
        dispose();
        //Volta mostrar o pai
        parent.setVisible(true);
    }//GEN-LAST:event_formWindowClosing

    private void excluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirActionPerformed

        int sele = list.getSelectedIndex();
        //Sem seleção
        if (sele == -1) {

            return;
        }

        if (showConfirmDialog(this, "Deseja proceder com a exclusão?",
                "CalpI - Cadastro de Alunos", YES_NO_OPTION) == NO_OPTION) {

            return;
        }

        con.removeElementBufferOut(sele);
        if (tipo == CURSOS) {
            
            
            Connectors.askWrite(Connectors.CURSOS);
        } else {

            Connectors.askWrite(Connectors.DISCIPLINAS);
        }
        //Atualiza
        refreshList();
    }//GEN-LAST:event_excluirActionPerformed

    private void editarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarActionPerformed

        int selecao = list.getSelectedIndex();
        if (selecao == -1) {

            return;
        }
        
        JInternalFrame jif = null;

        if (tipo == CURSOS) {

            jif = new TwoFaceFrameCadas(con.getElementAtBufferOut(selecao),
                    TwoFaceFrameCadas.CURSO);
        } else {

            jif = new TwoFaceFrameCadas(con.getElementAtBufferOut(selecao),
                    TwoFaceFrameCadas.DISCIPLINA);
        }

        JDialog d = new JDialog(this, true);
        d.setLocationRelativeTo(this);
        d.add(jif);
        d.setSize(300, 200);
        d.setResizable(false);
        jif.setVisible(true);
        d.setVisible(true);
        refreshList();
    }//GEN-LAST:event_editarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton editar;
    private javax.swing.JButton excluir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList list;
    // End of variables declaration//GEN-END:variables

}
